<template>
  <div class="wrap">
    <div class="search-wrap">
      <el-input
        v-model.trim="search.essaytitle"
        @keyup.enter="getEssayList"
        placeholder="模糊搜索短文标题"
        style="width: 220px"
        size="default"
      />
      <el-button
        class="search-class"
        size="default"
        type="primary"
        @click.stop="getEssayList"
        >查询</el-button
      >
    </div>
    <div class="table-header">
      <div>短文管理</div>
      <div>
        <el-button
          class="add"
          type="primary"
          size="default"
          :disabled="multipleSelection.length == 0"
          @click="batchRecovery"
          >批量恢复</el-button
        >

        <el-popconfirm
          title="确认要删除该列?"
          confirm-button-text="是"
          cancel-button-text="否"
          @confirm="batchDestroy"
        >
          <template #reference>
            <el-button
              class="add"
              type="primary"
              size="default"
              :disabled="multipleSelection.length == 0"
              >批量销毁</el-button
            >
          </template>
        </el-popconfirm>

      </div>
    </div>
    <el-table
      :data="essayList"
      style="width: 100%"
      :tbKey="tbKey"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="essaytitle" label="短文标题" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="user.username" label="来源"> </el-table-column>
      <el-table-column prop="created_at" label="创建时间" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="拥有书签" width="200">
        <template v-slot="scope">
          {{ scope.row.tags.map((tag) => tag.tagname).join("、") }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="350">
        <template v-slot="scope">
          <el-button
            size="small"
            type="primary"
            @click.stop="editorRow(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click.stop="releaseRow(scope.row)"
          >
            查看
          </el-button>
          <el-button size="small" @click="recovery(scope.row)">
            恢复
          </el-button>
          <el-popconfirm
            title="确认要删除该列?"
            confirm-button-text="是"
            cancel-button-text="否"
            @confirm="deleteRow(scope.row)"
          >
            <template #reference>
              <el-button size="small"> 销毁 </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-footer">
      <el-pagination
        background
        @current-change="currentChange"
        :page-size="pagination.pageSize"
        layout="prev, pager, next"
        :total="pagination.total"
      />
    </div>
  </div>
</template>

<script>
import request from "utils/request";
import { ElMessage } from "element-plus";

export default {
  name: "NestVueShortPassageRecyclingStation",

  data() {
    return {
      tbKey: 0,
      search: {
        essaytitle: "",
      },
      pagination: {
        currentPage: 1,
        pageSize: 5,
        total: 0,
      },
      essayList: [],
      multipleSelection: [],
    };
  },

  async mounted() {
    await this.getEssayList();
  },
  methods: {
    editorRow(row) {
      this.$router.push({
        path: "/write-notes",
        query: {
          id: row.id,
        },
      });
    },
    releaseRow(row) {
      window.open(
        "/blog/essay.html?id=" + row.id,
        "__blank"
      );
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async getEssayList() {
      let ret = await request.get("/essay/recycling-station/list", {
        essaytitle: this.search.essaytitle,
        currentPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      });
      if (ret.code == 200) {
        this.essayList = ret.data[0] || [];
        console.log("this.essayList", this.essayList);
        this.pagination.total = ret.data[1];
      }
    },
    async recovery(row) {
      let ret = await request.post(`/essay/batchRecovery`, {
        ids: [row.id],
      });
      if (ret.code == 200) {
        ElMessage.success("已恢复");
        this.getEssayList();
      }
    },
    async deleteRow(row) {
      let ret = await request.delete(`/essay/destroy/${row.id}`, {});
      if (ret.code == 200) {
        ElMessage.success("销毁成功");
        this.getEssayList();
      }
    },
    async currentChange(pageSize) {
      this.pagination.currentPage = pageSize;
      this.getEssayList();
    },
    async batchRecovery() {
      let ret = await request.post(`/essay/batchRecovery`, {
        ids: this.multipleSelection.map((item) => item.id),
      });
      if (ret.code == 200) {
        ElMessage.success("全部恢复");
        this.getEssayList();
      }
    },
    async batchDestroy() {
      let ret = await request.post(`/essay/batchDestroy`, {
        ids: this.multipleSelection.map((item) => item.id),
      });
      if (ret.code == 200) {
        ElMessage.success("全部销毁");
        this.getEssayList();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.wrap {
  .headPictureCol {
    width: 32px;
    height: 32px;
    overflow: hidden;
    border-radius: 50%;
  }
  .search-wrap {
    display: flex;
    margin-bottom: 15px;
    .search-class {
      margin-left: 20px;
    }
  }
  .table-header {
    display: flex;
    line-height: 50px;
    padding: 0 10px;
    justify-content: space-between;
    align-items: center;
  }
  .pagination-footer {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
